package com.tul.exercise.base.innerclass.chapter10_8.event;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description 《java编程思想》10.8
 *  控制框架：控制器
 *
 **/
public class Controller {

    // 事件列表
    private List<Event> eventList = new ArrayList<>();

    // 添加事件
    public void addEvent (Event c) {
        eventList.add(c);
    }

    public void run() {
        while(eventList.size() > 0) {
            /*eventList.forEach(event -> {
                if (event.ready()) {
                    System.out.println(event);
                    event.action();
                    eventList.remove(event);
                }
            });*/

            /*for(Event event:eventList) {
                if (event.ready()) {
                    System.out.println(event);
                    event.action();
                    eventList.remove(event);
                }
            }*/

           Iterator<Event> iter = eventList.iterator();

            while(iter.hasNext()) {
                Event event = iter.next();
                if(event.ready()){
                    System.out.println(event);
                    event.action();
                    iter.remove();
                }
            }


        }
    }
}
